//
// Created by hsj on 2024-1-10.
//

#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>

#ifdef WIN32
//windows minGW64
//inet_pton函数：_WIN32_WINNT
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0600
#include <winsock2.h>
#include <ws2tcpip.h>
//#pragma comment(lib,"ws2_32.lib")
#else
//linux  gcc
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/wait.h>
#include <signal.h>
#endif

#define BUF_SIZE 100
void error_handling(char *buf);

int main(int argc,char *argv[]){
    int sock;
    struct sockaddr_in recv_adr;
    char ip[]="127.0.0.1";
    char port[]="5555";

    sock=socket(PF_INET, SOCK_STREAM, 0);
    memset(&recv_adr, 0, sizeof(recv_adr));
    recv_adr.sin_family=AF_INET;
    recv_adr.sin_addr.s_addr=inet_addr(/*argv[1]*/ip) ;
    recv_adr.sin_port=htons (atoi(/*argv[2]*/port));

    if(connect( sock, (struct sockaddr*)&recv_adr, sizeof(recv_adr))==-1)
        error_handling("connect() error!");


    write(sock, "123", strlen("123"));
    send(sock,"4", strlen("4"), MSG_OOB);
    sleep(2);//sleep 2 sec
    write(sock,"567",strlen("567"));
    send(sock, "890", strlen("890"), MSG_OOB);

    close(sock);

    return EXIT_SUCCESS;
}
void error_handling( char *message){
    fputs (message, stderr);
    fputc('\n', stderr);
    exit(1);
}